//
//  GGPayViewHUD.m
//  test
//
//  Created by kepuG on 17/2/21.
//  Copyright © 2017年 test. All rights reserved.
//

#import "GGPayViewHUD.h"
#import "POP.h"


#define VWidth 200
#define VHeight 300
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;


#define BViewX (kScreen_Width - 200)*0.5
#define BViewY (kScreen_Height - 300)*0.5
#define BtnTag 100

const UIWindow *window_;


@interface GGPayViewHUD ()

@property (strong ,nonatomic) UIView *backView;

@end

@implementation GGPayViewHUD{
    
    UIView *redemmAlterView;
}

+(instancetype)sharedHUD{
    static dispatch_once_t onceToken;
    static GGPayViewHUD *hud = nil;
    dispatch_once(&onceToken, ^{
        hud = [[self alloc] init];
    });
    return hud;
}

/**
 * 创建window
 */


/**
* 显示支付视图
*/
+(void)show{

    GGPayViewHUD *hud = [self sharedHUD];
    window_ = [[UIWindow alloc] init];
    window_.frame = [UIScreen mainScreen].bounds;
    window_.backgroundColor = [UIColor colorWithHexString:@"#ffffff" andAlpha:0.8];
    window_.hidden = NO;
    
    [window_ addSubview:hud.backView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackbb)];
    [window_ addGestureRecognizer:tap];
    [hud animation:0.f to:2.f finish:nil];

}

/**
 * 隐藏支付视图
 */

+(void)dismiss{
    window_ = nil;
}

+(void)tapBackbb{

    [[GGPayViewHUD sharedHUD] animation:2.f to:0.f finish:^{
       
        [GGPayViewHUD dismiss];
    }];
}



#pragma mark -懒加载
-(UIView *)backView{
    if (_backView == nil) {
        
        _backView = [[UIView alloc] initWithFrame:CGRectMake(BViewX, BViewY, 200, 300)];
        _backView.backgroundColor= [UIColor whiteColor];
        
        NSArray *title = @[@"进程天津",@"钱包",@"支付宝",@"微信"];
        
        for (int i = 0; i<4; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor colorWithRed:arc4random()%2 green:arc4random()%2 blue:arc4random()%2 alpha:1.0];
            [btn setTitle:title[i] forState:UIControlStateNormal];
            btn.tag = i + BtnTag;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            CGRect fram = CGRectMake(0, i*300/4.0, 200, 300/4.0);
            btn.frame = fram;
            [_backView addSubview:btn];
        }
    }
    return _backView;
}

-(void)btnClick:(UIButton*)sender{
    DebugLog(@"%ld",sender.tag);
    
    if ([self.delegate respondsToSelector:@selector(ggPayViewHUDClickIndex:)]) {
        [self.delegate ggPayViewHUDClickIndex:sender.tag - BtnTag];
    }
}
/**
 * 淡入淡出动画
 */
-(void)animation:(CGFloat)from to:(CGFloat)to finish:(void(^)())finish{
    
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewAlpha];
    anSpring.fromValue = @(from);
    anSpring.toValue = @(to);
    // anSpring.beginTime = CACurrentMediaTime() + 1.0f;
    // anSpring.springBounciness = 0.7f;
    anSpring.springSpeed = 2.0f;
    [[GGPayViewHUD sharedHUD].backView pop_addAnimation:anSpring forKey:@"position"];
    
    
    if (finish) {
        finish();
    }

}
@end
